function Nash_equilibrium_grid
    % ... (other functions and code)

    % Load Welfare_max from the saved file
    load Results_NN.mat

    welfare_max = [GFT1' GFT3' shock13(position(:,2))' shockchi31(position(:,3))' shockchi33(position(:,5))'];
   %welfare_max(welfare_max(:, 3)< -0, :)= [] 
    % Find Nash equilibrium using the provided function
    [eq1, eq2] = findNashEquilibrium(welfare_max);

    % Check if (eq1, eq2) is a Nash equilibrium
 %    is_nash_equilibrium = checkNashEquilibrium(eq1, eq2, welfare_max);
% 
%     % Display the Nash equilibrium status
 %    if is_nash_equilibrium
        disp(['Nash Equilibrium: Country 1 - Instrument ' num2str(eq1)]);
         disp(['Nash Equilibrium: Country 2 - Instruments ' num2str(eq2)]);
  %   else
   %      disp('No Nash Equilibrium found.');
   %  end
end

function [eq1, eq2] = findNashEquilibrium(Welfare_max)
    % Extract instrument values and welfare for each country
    Instruments1 = Welfare_max(:, 3);
    Instruments2 = Welfare_max(:, 4:5);
    Welfare1 = Welfare_max(:, 1);
    Welfare2 = Welfare_max(:, 2);

    % Find the best response for country 1 given country 2's choice
    [~, idx1] = max(Welfare1(Instruments2(:, 1) == Instruments1));
    eq1 = Instruments1(idx1);

    % Find the best response for country 2 given country 1's choice
    valid_idx = find(Instruments2(:, 1) == eq1);
    [~, idx2] = max(Welfare2(valid_idx));
    eq2 = Instruments2(valid_idx(idx2), :);
end

function [is_nash_equilibrium, deviate1_idx, deviate2_idx] = checkNashEquilibrium(eq1, eq2, welfare_max)
    % Extract instrument values and welfare for each country
    Instruments1 = welfare_max(:, 3);
    Instruments2 = welfare_max(:, 4:5);
    Welfare1 = welfare_max(:, 1);
    Welfare2 = welfare_max(:, 2);

    % Check if country 1 has an incentive to deviate
    deviate1_idx = find(Instruments1 ~= eq1);
    [~, idx1_deviate] = max(Welfare1(deviate1_idx));
    deviate1_incentive = Welfare1(deviate1_idx(idx1_deviate)) > Welfare1(Instruments1 == eq1);

    % Check if country 2 has an incentive to deviate
    deviate2_idx = find(Instruments2(:, 1) ~= eq1);
    
    % Corrected comparison for the first instrument of country 2
    deviate2_incentive1 = Welfare2(deviate2_idx) > max(Welfare2(Instruments2(:, 1) == eq1 & all(Instruments2(:, 2:end) == eq2(1), 2)));

    % Corrected comparison for the second instrument of country 2
    deviate2_incentive2 = Welfare2(deviate2_idx) > max(Welfare2(Instruments2(:, 1) == eq1 & all(Instruments2(:, 2:end) == eq2(2), 2)));

 % The pair (eq1, eq2) is a Nash equilibrium if neither country has an incentive to deviate
% The pair (eq1, eq2) is a Nash equilibrium if neither country has an incentive to deviate
is_nash_equilibrium = ~any(deviate1_incentive) && ~any(deviate2_incentive1) && ~any(deviate2_incentive2);

if ~is_nash_equilibrium
    disp('No Nash Equilibrium found.');
    
    % Display deviating indices for Country 1
    if ~isempty(deviate1_idx)
        disp(['Country 1 Deviating Indices: ' num2str(deviate1_idx')]);
    else
        disp('Country 1 does not have an incentive to deviate.');
    end
    
    % Display deviating indices for Country 2
    if ~isempty(deviate2_idx(deviate2_incentive1))
        disp(['Country 2 Deviating Indices for Instrument 1: ' num2str(deviate2_idx(deviate2_incentive1)')]);
    else
        disp('Country 2 does not have an incentive to deviate for Instrument 1.');
    end
    
    if ~isempty(deviate2_idx(deviate2_incentive2))
        disp(['Country 2 Deviating Indices for Instrument 2: ' num2str(deviate2_idx(deviate2_incentive2)')]);
    else
        disp('Country 2 does not have an incentive to deviate for Instrument 2.');
    end
end


end

